package com.ff.xyh.consumer.dictionary.controller;


import com.ff.xyh.common.entity.dictionary.ComplaintStatus;
import com.ff.xyh.common.entity.dictionary.ComplaintType;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.consumer.dictionary.service.ComplaintStatusService;
import com.ff.xyh.consumer.dictionary.service.ComplaintTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 投诉状态表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-26
 */
@Api(tags = "举报相关接口")
@RestController
@RequestMapping("/dictionary")
public class ComplaintTypeController {

    @Autowired
    private ComplaintTypeService complaintTypeService;

    @ApiOperation(value = "获取所有举报类型信息")
    @GetMapping("get-complaint-type-all")
    public ResultObject getAllComplaintType() {
        List<ComplaintType> list = complaintTypeService.getAllComplaintType();
        Map map = new HashMap();
        map.put("items", list);
        return ResultObject.ok().data(map);
    }

    @ApiOperation(value = "根据id获取举报类型信息")
    @GetMapping("get-complaint-type-id")
    public ResultObject getComplaintTypeById(
            @RequestParam(name = "id", required = false) Integer id) {
        ComplaintType complaintType = complaintTypeService.getById(id);
        if (complaintType == null) {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            Map map = new HashMap();
            map.put("data", complaintType);
            return ResultObject.ok().data(map);
        }
    }

    @ApiOperation(value = "根据id删除举报类型信息")
    @GetMapping("delete-complaint-type-id")
    public ResultObject deleteComplaintTypeById (
            @RequestParam(name = "id", required = false) Integer id) {
        Map map = new HashMap();
        ComplaintType complaintType = complaintTypeService.getById(id);
        if ( complaintType == null ) {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            map.put("data", complaintType);
            if (complaintTypeService.deleteComplaintTypeById(id)) {
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        }
    }

    @ApiOperation(value = "添加举报类型信息")
    @PostMapping("insert-complaint-type")
    public ResultObject insertComplaintType (
            @RequestBody ComplaintType complaintType) {

        if ( complaintTypeService.getById(complaintType.getId()) != null ) {
            return ResultObject.error().message(ResultMessage.OBJECT_ALREADY_EXISTS);
        } else {
            if (complaintTypeService.insertComplaintType(complaintType)) {
                Map map = new HashMap();
                map.put("data", complaintType);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT_BUT_FAILED_TO_INSERT);
            }
        }
    }

    @ApiOperation(value = "修改审核状态信息")
    @PostMapping("update-complaint-type")
    public ResultObject updateComplaintType(
            @RequestBody ComplaintType complaintType) {
        if (complaintTypeService.getById(complaintType.getId()) == null) {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (complaintTypeService.updateComplaintType(complaintType)) {
                Map map = new HashMap();
                map.put("data", complaintType);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_UPDATE);
            }
        }
    }


}

